#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1010;
int arr[N][N];
int s[N][N];
int n, m, x, y;
int main()
{
	int t;
	cin >> t;
	while (t--) {
		cin >> n >> m >> x >> y;
		memset(arr, 0, sizeof(arr));
		memset(s, 0, sizeof(s));
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				cin >> arr[i][j];
				s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + arr[i][j];
				//cout << s[i][j] << " ";
			}
			//cout << endl;
		}
		int ma = 0;
		for (int i = x; i <= n; i++) {
			for (int j = y; j <= m; j++) {
				ma = max(ma, s[i][j] - s[i - x][j] - s[i][j - y] + s[i - x][j - y]);
			}
		}
		cout << ma << endl;
	}
	return 0;
}